package day15.socketdemo08;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * @author Xiao
 * @date 2021/8/9
 */
public class SocketThread implements Runnable{
    private Socket so;
    public SocketThread(Socket so) {
        this.so = so;
    }
    @Override
    public void run() {
        BufferedWriter bw = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    so.getInputStream()));
            bw = new BufferedWriter(new FileWriter(
                    "videoPractice\\src\\day15\\socketdemo08\\"
                            + UUID.randomUUID().toString().replaceAll(
                                    "-", "") + ".txt"));
            String s;
            while ((s = br.readLine()) != null) {
                bw.write(s);
                bw.newLine();
            }
            bw.flush();
            BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(
                    so.getOutputStream()));
            bw1.write("已经完成上传工作，Over！");
            bw1.newLine();
            bw1.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (so != null) {
                try {
                    so.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
